﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;

using System.Text.Json;
using System.Windows.Forms;

namespace Disk
{
    class Upload
    {
        public static int AAD(string fileName)
        {
            int num = Up(fileName);
            if (num == 1)
            {
                return 1;
            }
            if (num == 2)
            {
                return 2;
            }
            return 3;
        }
        //上传文件
        public static int Up(string fileName)
        {

            //头部第一行的固定格式
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            //创建WebRequest请求
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://localhost:55442/api/upload");
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;

            Stream rs = wr.GetRequestStream();
            rs.Write(boundarybytes, 0, boundarybytes.Length);//写入请求头

            //开始文件上传格式构造
            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            string header = string.Format(headerTemplate, "file", Path.GetFileName(fileName), "application/octet-stream");
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);
            //结束文件上传格式构造

            //开始传输文件
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            //循环读取文件内容并写入网络流中
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, bytesRead);

            }
            fileStream.Close();

            //开始文件上传结尾构造
            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();
            //结束文件上传结尾构造

            //开始获取服务器返回
            WebResponse wresp = null;
            try
            {
                wresp = wr.GetResponse();
                Stream stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
                bool sum = Convert.ToBoolean(reader2.ReadToEnd());
                reader2.Close();
                stream2.Close();
                wresp.Close();
                //失败
                if (sum == false)
                {
                    return 1;
                }
                //成功
                if (sum == true)
                {
                    return 2;
                }
                return 3;


            }
            catch (Exception ex)
            {
                if (wresp != null)
                {
                    wresp.Close();
                    wresp = null;
                }
            }
            finally
            {
                wr = null;
            }
            return 3;
        }

        //上传账号密码进行验证登录
        public static int Login(string username, string password)
        {
            string url = "http://localhost:55442/api/updata";
            WebClient ds = new WebClient();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("username", $"{username}");
            nvc.Add("password", $"{ password}");

            byte[] response = ds.UploadValues(url, nvc);
            int result = Convert.ToInt32(Encoding.UTF8.GetString(response));

            if (result == 0)
            {
                return 0;
            }
            return 3;



        }

        //调用显示文件列表方法
        public static void Show(int Allfile)
        {
            string url = "http://localhost:55442/api/Show";
            WebClient ds = new WebClient();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("allfile", $"{Allfile}");
            byte[] response = ds.UploadValues(url, nvc);
            string result = Encoding.UTF8.GetString(response);   
            var json3 = JsonSerializer.Deserialize<List<Filess>>(result);
            login.firstpage.dataGridView1.DataSource = json3;
            
       

        }
        //注册
        public static bool Register(string username,string password,string secret,string answer) 
        {
            string url = "http://localhost:55442/api/Register";
            WebClient ds = new WebClient();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("username", $"{username}");
            nvc.Add("password", $"{ password}");
            nvc.Add("secret", $"{ secret}");
            nvc.Add("answer", $"{ answer}");
            byte[] response = ds.UploadValues(url, nvc);
            int result = Convert.ToInt32(Encoding.UTF8.GetString(response));
            if (result==1)
            {
                return true;
            }
            else
            {
                return false;
            }


        }
        //找回密码
        public static bool Retrieve(string username, string oldpassword,string newpassword, string secret, string answer)
        {
            string url = "http://localhost:55442/api/Retrieve";
            WebClient ds = new WebClient();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("username", $"{username}");
            nvc.Add("oldpassword", $"{ oldpassword}");
            nvc.Add("newpassword", $"{ newpassword}");
            nvc.Add("secret", $"{ secret}");
            nvc.Add("answer", $"{ answer}");
            byte[] response = ds.UploadValues(url, nvc);
            bool result =Convert.ToBoolean( Encoding.UTF8.GetString(response));
            if (result == true)
            {
                return true;
            }
            else
            {
                return false;
            }


        }



    }
}
